﻿using EdiNet;
using Xunit;
using System;
using System.IO;

namespace EdiNet.Test
{


    public class EdiStreamWriterTest
    {



        /// <summary>
        ///A test for EdiStreamWriter Constructor
        ///</summary>
        [Fact()]
        public void EdiStreamWriterConstructorTest()
        {
            Stream ediStream = new MemoryStream();
            EdiReaderWriterSettings settings = EdiReaderWriterSettings.DefaultX12;
            EdiStreamWriter target = new EdiStreamWriter(ediStream, settings);

        }

        /// <summary>
        ///A test for Close
        ///</summary>
        [Fact()]
        public void CloseTest()
        {
            MemoryStream ediStream = new MemoryStream();
            EdiReaderWriterSettings settings = EdiReaderWriterSettings.DefaultX12;
            EdiStreamWriter target = new EdiStreamWriter(ediStream, settings);
            target.Close();
            Assert.Throws(
                typeof(ObjectDisposedException), delegate()
                {
                    ediStream.WriteByte(0);
                }
            );
        }

        /// <summary>
        ///A test for Dispose
        ///</summary>
        [Fact()]
        public void DisposeTest()
        {
            Stream ediStream = new MemoryStream();
            EdiReaderWriterSettings settings = EdiReaderWriterSettings.DefaultX12;
            EdiStreamWriter target = new EdiStreamWriter(ediStream, settings);
            target.Dispose();
            Assert.Throws<ObjectDisposedException>(delegate()
            {
                ediStream.WriteByte(0); //throw System.ObjectDisposedException exception
            });
        }

        /// <summary>
        ///A test for WriteSegment
        ///</summary>
        [Fact()]
        public void WriteSegmentTest()
        {
            MemoryStream ediStream = new MemoryStream();
            EdiReaderWriterSettings settings = EdiReaderWriterSettings.DefaultX12;
            EdiStreamWriter target = new EdiStreamWriter(ediStream, settings);
            DataSegment seg = new DataSegment("SEG");
            seg[1][0] = "AA";
            seg[2][0] = "BB";
            seg[2][1] = "CC";
            seg[5][0] = "DD";
            seg[7][3] = "EE";
            target.WriteSegment(seg);
            target.Close();
            string res = StreamHelper.StringFromAray(ediStream.ToArray());
            Assert.Equal("SEG*AA*BB:CC***DD**:EE~", res);
        }
    }
}
